package com.gxuwz.leaveSystem.controller;

import com.gxuwz.leaveSystem.entity.Classes;
import com.gxuwz.leaveSystem.entity.Course;
import com.gxuwz.leaveSystem.service.ClassesService;
import com.gxuwz.leaveSystem.service.CourseService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class CourseController {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    CourseService courseService = (CourseService) applicationContext.getBean("courseService");

    /**
     * 根据id删除
     * @param id
     */
    @RequestMapping("/course_delete")
    public ModelAndView delete(String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/course_list");
        try {
            int num = courseService.delete(id);
            if (num == 1){
                System.out.println("删除成功");
                return mv;
            }else {
                System.out.println("删除失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }
    /**
     * 修改
     * @param course
     * @return
     */
    @RequestMapping("/course_edit")
    public ModelAndView update(Course course){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/course_list");
        try {
            int num = courseService.update(course);
            if (num==1){
                System.out.println("更新成功");
                return mv;
            }else {
                System.out.println("更新失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return mv;
    }

    /**
     * 转到编辑页面
     * @param id
     * @return
     */
    @RequestMapping("/course_editView")
    public ModelAndView editView(String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/course/course_edit");
        try {
            Course course = courseService.findByid(id);
            mv.addObject("course",course);
            return mv;
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }


    /**
     * 添加
     * 判断
     * 重定向到列表页面
     * @param course
     * @return
     */
    @RequestMapping("/course_add")
    public ModelAndView add(Course course){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/course_list");
        try {
            int num = courseService.add(course);
            if (num == 1){
                System.out.println("添加成功");
                return mv;
            }else {
                System.out.println("添加失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * 定向到添加页面
     * @return
     */
    @RequestMapping("/course_addView")
    public ModelAndView addView(){
        ModelAndView mv = new ModelAndView();
        Course course = new Course("1","2024","软件架构","4","1",4);
        mv.addObject("course",course);
        mv.setViewName("/course/course_add");
        return mv;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("/course_list")
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/course/course_list");
        try {
            List<Course> courses = courseService.findAll();
            mv.addObject("course",courses);
            return mv;
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return
     */
    @RequestMapping("/course_search")
    public ModelAndView search(String keyword) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/course/course_list");
        try {
            List<Course> courses = courseService.search(keyword);
            if (keyword!=null&&courses!=null){
                mv.addObject("course",courses);
                return mv;
            }else {
                System.out.println("查找不到或没有！");
            }
        }catch (Exception e){
            System.out.println("服务器异常");
            return null;
        }
        return mv;
    }

}
